﻿/* Replace all hyperlinks with RTEObjectLink with contents of their title attribute*/ 
function replaceRTEObjects() {
    if (document.forms['aspnetForm']['MSOLayout_InDesignMode'] == null || document.forms['aspnetForm']['MSOLayout_InDesignMode'].value != "1") 
    {
        var elems = getElementsByClassName(document, 'a', 'RTEObjectLink');
        var obj;
        var parent;
        var content;
        var id;
        for (var i = 0, len = elems.length; i < len; i++) {
            parent = elems[i].parentNode;
            content = fixEmbeddedVideo(elems[i].title);
            obj = document.createElement('span');
            obj.innerHTML = content;
            parent.replaceChild(obj, elems[i]);
        }
    }

}

/*Return array of all elements with specified class name*/
function getElementsByClassName(oElm, strTagName, strClassName) {
    var arrElements = (strTagName == "*" && document.all) ? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for (var i = 0; i < arrElements.length; i++) {
        oElement = arrElements[i];
        if (oRegExp.test(oElement.className)) {
            arrReturnElements.push(oElement);
        }
    }
    return (arrReturnElements);
}

/*Add class id to object if it does not already exist as this is required for IE6*/
function fixEmbeddedVideo(embedCode) {
    if (embedCode && embedCode.toLowerCase().indexOf("classid") == -1) {
        var objPos = embedCode.toLowerCase().indexOf("object") + "object ".length;
        return embedCode.substr(0, objPos) + "classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'" + embedCode.substr(objPos);
    } else {
        return embedCode;
    }
}

/*Add function to run on page load*/
function myWindowOnload(func) {
    if(window.addEventListener) {
        // See if the browser has the function addEventListener
       window.addEventListener('load', func, false);
    } else if(window.attachEvent) {
        // See if the browser has the function attachEvent
       window.attachEvent('onload', func);
    } 
}

myWindowOnload(replaceRTEObjects);